
import { gg } from "../../../../Game";
import { GameplayComponent } from "../../GameplayComponent";
import ViewGameplay from "../../ViewGameplay";
import { Comp } from "../../m/CompDef";
import { GpEventDef } from "../../m/GpEventDef";
import { GpCmdSystem, GpCmdType, GpCmdDataDef } from "../../m/systems/CmdSystem";
import { GpEntity } from "../GpEntity";
import GpHero from "../GpHero";
import GpActionPanel from "./GpActionPanel";
import GpActionTouchLayer from "./GpActionTouchLayer";


import { _decorator, Component, Node, v3, Vec3 } from 'cc';
const { ccclass, property } = _decorator;
@ccclass
export default class GpControlPanel extends GameplayComponent<ViewGameplay>{
    currentControlEntityId: number = 0
    gpActionPanel: GpActionPanel
    gpActionTouchLayer: GpActionTouchLayer

    onInit(): void {
        this.gpActionPanel = this.node.getComponentInChildren(GpActionPanel)
        this.gpActionPanel.init(this)
        this.gpActionTouchLayer = this.node.getComponentInChildren(GpActionTouchLayer)
        this.gpActionTouchLayer.init(this)
        const allHero = this.prop.ecsWorld.getGroupByClz(Comp.Hero)
        if (allHero.entities.length > 0) {
            const myHero = allHero.entities.find(i => i.component(Comp.Force).value == 0)
            if (myHero) {
                this.currentControlEntityId = myHero.id
            }
        }
    }

    protected onDestroy(): void {
        gg.eventTarget.targetOff(this)
    }

    lastJoystickDirection: Vec3 = v3(0, 0, 0)

    // onJoyStickMove(stick: VisualJoyStick, info: { direction: Vec3, radian: number }) {
    //     if (info.direction.equals(this.lastJoystickDirection)) {
    //         return
    //     }

    //     this.setDirection(info.direction.clone())
    //     // console.log("onJoyStickMove", info.direction.x, info.direction.y);

    //     this.lastJoystickDirection.set(info.direction)

    // }


    setDirection(direction: Vec3) {
        // if (direction.x == 0 && direction.y == 0) {
        //     //方向为0
        //     return
        // }
        const currentControlEntity = this.prop.ecsWorld.entities[this.currentControlEntityId]
        if (!currentControlEntity) {
            //实体不存在
            return
        }
        const targetDirection = currentControlEntity.component(Comp.Move).targetDirection
        if (direction.x == targetDirection.x && direction.y == targetDirection.y) {
            /**方向没变化 */
            return
        }

        this.prop.ecsWorld.system(GpCmdSystem).makeCmd(
            GpCmdType.changeTargetDirection,
            new GpCmdDataDef.ChangeTargetDirection(
                currentControlEntity.id,
                direction
            )
        )
    }

    protected update(dt: number): void {
        if (!this.prop) return
        if (!this.currentControlEntityId) {
            const currentControlEntityId = 10000// this.prop.gpWorld.prop.ecsWorld.getGroupByClz(Comp.Hero).entities.find(i=>i.id==10000)
            this.currentControlEntityId = currentControlEntityId
        } else {
            const gpEntity = this.prop.gpWorld.entityMap.get(this.currentControlEntityId)
            if (gpEntity) {

            }

        }
    }
}